package cn.tom.org.controller;

import cn.tom.basic.util.AjaxResult;
import cn.tom.org.pojo.domain.Department;
import cn.tom.org.pojo.query.DepartmentQuery;
import cn.tom.org.service.DepartmentService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/department")
@Api(tags = "部门模块")
public class DepartmentController {

    @Autowired
    private DepartmentService service;

    @GetMapping("/{id}")
    public Department getOne(@PathVariable("id")Long id) {
        return service.getOne(id);
    }

    @GetMapping
    public List<Department> getAll() {
        return service.getAll();
    }

    @PutMapping
    public AjaxResult save(@RequestBody Department department) {
        if(department.getId() == null){
            service.add(department);
        }else{
            service.update(department);
        }
        return AjaxResult.ok();
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")Long id) {
        service.delete(id);
        return AjaxResult.ok();
    }

    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        service.patchDelete(ids);
        return AjaxResult.ok();
    }

    @PostMapping
    public PageInfo<Department> queryPage(@RequestBody DepartmentQuery query) {
        return service.queryPage(query);
    }

    @GetMapping("/deptTree")
    public List<Department> deptTree() {
        return service.deptTree();
    }

}
